package rbac

import (
	"github.com/gin-gonic/gin"
	"sort"
	"store/libs/container"
	"store/models/rbac"
)

func GetRightsIdList(ctx *gin.Context, adminId uint64) map[uint64]uint8 {
	rights := map[uint64]uint8{}
	//获取所有启用的用户角色
	userRoles := GetAllEnableUserRoles(ctx)
	if userRoles == nil {
		return rights
	}

	//获取管理员的角色列表
	roles, exists := userRoles[adminId]
	if !exists || len(roles) < 1 {
		return rights
	}

	//获取所有角色权限
	rightsList := GetAllEnableAccess(ctx)
	if rightsList == nil {
		return rights
	}

	//获取管理员所有角色拥有权限id列表
	for _, roleId := range roles {
		ids, exists := rightsList[roleId]
		if exists {
			for _, id := range ids {
				rights[id] = 1
			}
		}
	}

	return rights
}

/**
 * 重置所有启用角色
 */
func ResetAllEnableRoles(ctx *gin.Context) []rbac.AdminRole {
	roleList := rbac.AllEnableRoles(ctx)
	if len(roleList) > 0 {
		container.Set("rbac:allRoles", roleList)
	}
	return roleList
}

/**
 * 获取所有启用角色
 */
func GetAllEnableRoles(ctx *gin.Context) []rbac.AdminRole {
	list := container.Get("rbac:allRoles")
	if list != nil {
		roleList, ok := list.([]rbac.AdminRole)
		if ok {
			return roleList
		}
	}

	return ResetAllEnableRoles(ctx)
}

/**
 * 重置所有管理角色
 */
func ResetAllEnableUserRoles(ctx *gin.Context) map[uint64][]uint64 {
	userRoleList := rbac.AllEnableUserRoles(ctx)
	if len(userRoleList) > 0 {
		container.Set("rbac:allUserRoles", userRoleList)
	}
	return userRoleList
}

/**
 * 获取所有管理员角色
 */
func GetAllEnableUserRoles(ctx *gin.Context) map[uint64][]uint64 {
	list := container.Get("rbac:allUserRoles")
	if list != nil {
		userRoles, ok := list.(map[uint64][]uint64)
		if ok {
			return userRoles
		}
	}

	return ResetAllEnableUserRoles(ctx)
}

type RightsTree struct {
	Children []*RightsTree
	Data     *rbac.AdminRights
}

/**
 * 重置所有权限
 */
func ResetAllEnableRights(ctx *gin.Context) *RightsTree {
	rightsList := rbac.AllEnableRights(ctx)
	tree := buildRightsTree(rightsList)

	if len(rightsList) > 0 {
		container.Set("rbac:allRights", tree)
	}
	return tree
}

/**
 * 构建权限树
 */
func buildRightsTree(list []rbac.AdminRights) *RightsTree {
	tree := new(RightsTree)
	if len(list) < 1 {
		return tree
	}

	//按照level ASC，id ASC排序
	sort.Sort(rbac.NewSortRights(list, rbac.ByLevel))
	existsMap := map[uint64]*RightsTree{}

	for index, _ := range list {
		right := list[index]
		current := &RightsTree{
			Children: []*RightsTree{},
			Data:     &right,
		}

		existsMap[right.Id] = current

		if right.Level > 0 {
			if parent, ok := existsMap[right.ParentId]; ok {
				parent.Children = append(parent.Children, current)
			}
		} else { //根，只能有一个
			tree = current
		}
	}
	return tree
}

/**
 * 获取所有权限
 */
func GetAllEnableRights(ctx *gin.Context) *RightsTree {
	list := container.Get("rbac:allRights")
	if list != nil {
		rightsList, ok := list.(*RightsTree)
		if ok {
			return rightsList
		}
	}

	return ResetAllEnableRights(ctx)
}

/**
 * 重置所有权限
 */
func ResetAllEnableAccess(ctx *gin.Context) map[uint64][]uint64 {
	accessList := rbac.AllEnableAccess(ctx)
	if len(accessList) > 0 {
		container.Set("rbac:allAccess", accessList)
	}
	return accessList
}

/**
 * 获取所有权限
 */
func GetAllEnableAccess(ctx *gin.Context) map[uint64][]uint64 {
	list := container.Get("rbac:allAccess")
	if list != nil {
		accessList, ok := list.(map[uint64][]uint64)
		if ok {
			return accessList
		}
	}

	return ResetAllEnableAccess(ctx)
}
